package uform.model.core;

/**
 * ...
 * @author Franco Ponticelli
 */

import thx.error.Error;
import thx.error.NullArgument;
import uform.model.cell.Cell;
using Strings;

class Field<TType, TContainer>
{
	public var name(default, setName) : String;
	public var cell(default, setCell) : Cell<TType, TContainer>;
	public var title(getTitle, null) : String;
	public var description : String;
	public var defaultValue : Null<TType>;
	public function new(name : String, cell : Cell<TType, TContainer>, ?title : String, ?defaultValue : TType, ?description : String)
	{
		this.name = name;
		this.cell = cell;
		this.title = title;
		this.defaultValue = defaultValue;
		this.description = description;
	}
	
	function setName(value : String)
	{
		NullArgument.throwIfNull(value, "value");
		value = StringTools.trim(value);
		if ("" == value)
			throw new Error("field name can't be an empty string");
		return name = value;
	}
	
	function setCell(value : Cell<TType, TContainer>)
	{
		NullArgument.throwIfNull(value, "value");
		return cell = value;
	}
	
	function getTitle()
	{
		if (null == title)
			return name.humanize();
		else
			return title;
	}
	
	public function toString()
	{
		return title + " (" + name + "): " + cell.toString();
	}
}